package controller.product;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Observable;

import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

import model.Supplier;
import view.product.ProductSupplierPanel;
import controller.DoCheck;
import dao.SupplierDAO;

public class SupplierControl extends Observable implements ActionListener,
		MouseListener {

	private SupplierDAO supDAO;
	private DoCheck check;
	private ProductSupplierPanel proSup;

	public SupplierControl(ProductSupplierPanel pro) {
		this.proSup = pro;
		supDAO = new SupplierDAO();
		check = new DoCheck();
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		String id = (String) proSup.getTblProdsupListsupplier().getValueAt(
				proSup.getTblProdsupListsupplier().getSelectedRow(), 0);
		Supplier supplier = (Supplier) supDAO.searchSupplier(id);
		proSup.getTxtProdsupId().setText(supplier.getId());
		proSup.getTxtProdsupSuppliername().setText(supplier.getName());
		proSup.getTxtaProdsupAddress().setText(supplier.getAddress());
		proSup.getTxtProdsupPhone().setText(supplier.getPhone());
		;

	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == proSup.getBtnProdsupAdd()) {
			doSave(e);
		} else if (e.getSource() == proSup.getBtnProdsupDelete()) {
			doDelete(e);
		} else if (e.getSource() == proSup.getBtnProdsupEdit()) {
			doLoad(e);
		} else if (e.getSource() == proSup.getBtnProdsupDelete()) {
			doDelete(e);
		} else if (e.getSource() == proSup.getBtnProdsupUpdate()) {
			doUpdate(e);
		} else if (e.getSource() == proSup.getBtnProdsupSearch()) {
			doSearch(e);
		} else if (e.getSource() == proSup.getBtnProdsupCancel()) {
			doReset(e);
		}
	}

	private void doReset(ActionEvent e) {
		proSup.getTxtProdsupSuppliername().setText("");
		proSup.getTxtProdsupId().setText("");
		proSup.getTxtaProdsupAddress().setText("");
		proSup.getTxtProdsupPhone().setText("");
		proSup.getTxtProdsupSearch().setText("");

		proSup.getBtnProdsupEdit().setEnabled(false);
		proSup.getBtnProdsupUpdate().setEnabled(false);
		proSup.getBtnProdsupDelete().setEnabled(false);
	}

	public void doSave(ActionEvent event) {
		Supplier supplier = new Supplier();
		String id = proSup.getTxtProdsupId().getText();
		supplier.setId(id);
		supplier.setName(proSup.getTxtProdsupSuppliername().getText());
		supplier.setAddress(proSup.getTxtaProdsupAddress().getText());
		String phone = proSup.getTxtProdsupPhone().getText();
		supplier.setPhone(phone);

		if (check.doCheckPerId(id) && check.doCheckPhone(phone)
				&& supDAO.insertSupplier(supplier)) {
			JOptionPane.showMessageDialog(null, "Insert Supplier successfull!");
			doLoad(event);
			setChanged();
			notifyObservers();
		} else {
			JOptionPane.showMessageDialog(null, "Insert Supplier fail!");
		}

	}

	public void doDelete(ActionEvent event) {
		int check = JOptionPane.showConfirmDialog(null,
				"Do you want to delete?", "Delete", JOptionPane.YES_NO_OPTION);
		if (check == JOptionPane.YES_OPTION) {
			String supid = (String) proSup
					.getTblProdsupListsupplier()
					.getValueAt(
							proSup.getTblProdsupListsupplier().getSelectedRow(),
							0);
			supDAO.deleteSupplier(supDAO.searchSupplier(supid));
			JOptionPane.showMessageDialog(null, "Delete Supplier successfully");
			doLoad(event);
			setChanged();
			notifyObservers();
		} else if (check == JOptionPane.CANCEL_OPTION) {
		}
	}

	public void doUpdate(ActionEvent event) {
		String id = (String) proSup.getTblProdsupListsupplier().getValueAt(
				proSup.getTblProdsupListsupplier().getSelectedRow(), 0);
		Supplier supplier = supDAO.searchSupplier(id);
		supplier.setName(proSup.getTxtProdsupSuppliername().getText());
		supplier.setAddress(proSup.getTxtaProdsupAddress().getText());
		String phone = proSup.getTxtProdsupPhone().getText();
		supplier.setPhone(phone);
		if (supDAO.updateSupplier(supDAO.searchSupplier(id))
				&& check.doCheckPhone(phone)) {
			JOptionPane.showMessageDialog(null, "update supplier successfully");
			doLoad(event);
		} else {
			JOptionPane.showMessageDialog(null, "update supplier fail");
		}

	}

	public void doLoad(ActionEvent event) {
		DefaultTableModel dtm = new DefaultTableModel();
		dtm.addColumn("ID");
		dtm.addColumn("Name");
		dtm.addColumn("Phone");
		dtm.addColumn("Address");
		for (Supplier i : supDAO.showAllSupplier()) {
			dtm.addRow(new Object[] { i.getId(), i.getName(), i.getPhone(),
					i.getAddress() });
			proSup.getTblProdsupListsupplier().setModel(dtm);
		}

	}

	public void doSearch(ActionEvent event) {
		String key = proSup.getTxtProdsupSearch().getText();
		DefaultTableModel dtm = new DefaultTableModel();
		dtm.addColumn("ID");
		dtm.addColumn("Name");
		dtm.addColumn("Phone");
		dtm.addColumn("Address");
		for (Supplier supplier : supDAO.showAllSupplierByAll(key)) {
			dtm.addRow(new Object[] { supplier.getId(), supplier.getName(),
					supplier.getPhone(), supplier.getAddress() });
		}
		proSup.getTblProdsupListsupplier().setModel(dtm);

	}

}
